<?php
/**
 * Create by 11li
 * Date 2020/2/11
 * Time 11:06
 *
 */

namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\library\MyWeixin;
use app\common\service\UserService;

class Smallapp extends Api
{
	
	/**
	 * 创建小程序码
	 */
	public function createQrCode() {
		$path = urldecode($this->request->post('path'));
		$weixin = MyWeixin::getInstance();
		$url = $weixin->createQrCode($path);
		if($url) {
			$this->success('创建成功'.$path, $url);
		} else {
			$this->error('二维码创建出错');
		}
	}
	
	/**
	 * 授权用户资料
	 */
	public function authUserInfo()
	{
		$data = [];
		$nickname = $this->request->post('nickName');
		$avatar = $this->request->post('avatarUrl');
		$userId = $this->auth->id;
		if(!$nickname && !$avatar) {
			$this->error('参数错误');
		}
		
		$nickname && $data['nickname'] = $nickname;
		$avatar && $data['avatar'] = $avatar;
		$data['auth_info'] = 1;
		$res = (new UserService())->updateById($userId, $data);
		if($res) {
			$userInfo = $this->auth->getUserinfo();
			$this->success('更改成功', array_merge($userInfo, $data));
		} else {
			$this->error('更改出错');
		}
	}
}